package com.aspyn.servicestest;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ServicesTestActivity extends Activity {

	Service appService;
	Context myContext;

	private ServiceConnection onService = new ServiceConnection() {
		public void onServiceConnected(ComponentName className,IBinder rawBinder) {
			appService = ((TestService.LocalBinder) rawBinder).getService();
		}
		public void onServiceDisconnected(ComponentName className) {
			appService = null;
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		myContext = this;
		
		bindService(new Intent(this, TestService.class), onService, Context.BIND_AUTO_CREATE);//You need to bind to a service before starting it
		
		Button btn = (Button)findViewById(R.id.btn1);
		btn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub 
				System.out.println(appService);
				appService.startService(new Intent(myContext,TestService.class));
			}

		});
	}


}